﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using CommonLib;
using System.Data;
using YouErYuanModels;

namespace YouErYuanBLL
{
    public class DictionaryBLL
    {

        public object GetDictionaryList()
        {
            //课程
            //string sql01 = "SELECT [id],[pid],[name],[type] FROM [dbo].[T_Dictionary] where pid=10000;";
            //食谱
            //string sql02 = "SELECT [id],[pid],[name],[type] FROM [dbo].[T_Dictionary] where pid=20000;";
            //餐类型
            string sql03 = "SELECT [id],[pid],[name],[type] FROM [dbo].[T_Dictionary] where pid=30000;";
            //string sql = sql01 + sql02 + sql03;
            DataSet ds = SQLHepler.GetDataSet(sql03);
            //List<object> list = new List<object>();
            //if (ds != null && ds.Tables.Count > 0)
            //{
            //    list.Add(ds.Tables[0]);
            //    list.Add(ds.Tables[1]);
            //    list.Add(ds.Tables[2]);
            //}
            DataTable dt = SQLHepler.GetDataTable(sql03);
            return dt;
        }
        /// <summary>
        /// 新增或删除
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public int UpdateData(Dictionary datas, string action)
        {
            //删除
            string sqlDel = "delete T_Dictionary where id=@id";
            //新增
            string sqlAdd = "insert into T_Dictionary(id,pid,name,type)values(@id,@pid,@name,@type)";
            //验证是否重复
            string sqlValid = "select count(1) from T_Dictionary where name=@name and type=@type";
            string sql = action == "Del" ? sqlDel : sqlAdd;
            SqlParameter[] par = new SqlParameter[] {
                new SqlParameter("@id",datas.Id),
                new SqlParameter("@pid",datas.Pid),
                new SqlParameter("@name",datas.Name),
                new SqlParameter("@type",datas.Type)
            };
            if (Convert.ToInt32(SQLHepler.GetSingleValue(sqlValid,par))>0 && action!= "Del")
            {
                return -2;
            }
            return SQLHepler.ExecuteSql(sql,par);
        }

        /// <summary>
        /// 得到同类型最大id
        /// </summary>
        /// <returns></returns>
        public string getMaxId(Dictionary datas)
        {
            string sql = "select max(id) from t_Dictionary where type=@type";
            SqlParameter[] par = new SqlParameter[] {
                new SqlParameter("@type",datas.Type)
            };
            return SQLHepler.GetSingleValue(sql, par)+"";
        }
    }
}
